package cn.onein.edu.web.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.onein.edu.biz.bean.LearnSection;
import cn.onein.edu.biz.bean.User;
import cn.onein.edu.util.DateUtil;
import cn.onein.edu.web.service.LearnService;

public class CenterHotContentUtil {

	private final static String YHQ_KEY = "yhq";

	private final static String ZEROTOONE_KEY = "0to1";

	private final static String ONETOTHREE_KEY = "1to3";

	private final static String THREETOSIX_KEY = "3to6";
	
	private final static int LIMIT_COUNT = 12;

	public static void initHotContent(HttpServletRequest req, User user,
			LearnService learnService, Map<String, Object> param) {
		Map<String, Object> map = null;
		int age = DateUtil.birthday2Age(user.getBirthday());
		
		if (age == -1) {
			map = learnService.getHotContentBySectionName(YHQ_KEY, LIMIT_COUNT);
		} else if (age == 0) {
			map = learnService.getHotContentBySectionName(ZEROTOONE_KEY, LIMIT_COUNT);
		} else if (age > 0 && age < 3){
			map = learnService.getHotContentBySectionName(ONETOTHREE_KEY, LIMIT_COUNT);
		} else {
			map = learnService.getHotContentBySectionName(THREETOSIX_KEY, LIMIT_COUNT);
		}

		LearnSection section = (LearnSection) map.get("section");
		
		if (section != null) {
			String moreContentUrl = req.getRequestURL().toString();
			
			moreContentUrl = moreContentUrl.substring(0, moreContentUrl.indexOf("center"));
			
			moreContentUrl = moreContentUrl + "learn/" + section.getName();
			
			param.put("moreContentUrl", moreContentUrl);
		}
		
		param.put("learnContents", map.get("hotContents"));
	}
}
